﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Net;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using System.Configuration;
using Microsoft.JScript;

namespace aliformerp
{
    public struct acctk
    {
        public string uid;
        public string aliId;
        public string resource_owner;
        public string memberId;
        public string expires_in;
        public string refresh_token;
        public string access_token;
    }
    public class userinfo
    {
        public static acctk acctk2;
        public static string appKey = Properties.Settings.Default.appKey;
        public static string site = Properties.Settings.Default.site;
        public static string secretkey = Properties.Settings.Default.secretkey;
        public static string redirect_uri = Properties.Settings.Default.redirect_uri;
        public static string account = Properties.Settings.Default.account;
        public static string password = Properties.Settings.Default.password;

        public static string sign(Dictionary<string, string> paramDic, string appSecret)
        {
            byte[] signatureKey = Encoding.UTF8.GetBytes(appSecret);
            //第一步：拼装key+value
            List<string> list = new List<string>();
            foreach (KeyValuePair<string, string> kv in paramDic)
            {
                list.Add(kv.Key + kv.Value);
            }
            //第二步：排序
            list.Sort();
            //第三步：拼装排序后的各个字符串
            string tmp = "";
            foreach (string kvstr in list)
            {
                tmp = tmp + kvstr;
            }
            //第四步：将拼装后的字符串和app密钥一起计算签名
            //HMAC-SHA1
            HMACSHA1 hmacsha1 = new HMACSHA1(signatureKey);
            hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(tmp));
            byte[] hash = hmacsha1.Hash;
            //TO HEX
            return BitConverter.ToString(hash).Replace("-", string.Empty).ToUpper();
        }
        public static string sign(string urlPath, Dictionary<string, string> paramDic)
        {
            byte[] signatureKey = Encoding.UTF8.GetBytes(secretkey);//此处用自己的签名密钥
            List<string> list = new List<string>();
            foreach (KeyValuePair<string, string> kv in paramDic)
            {
                list.Add(kv.Key + kv.Value);
            }
            list.Sort();
            string tmp = urlPath;
            foreach (string kvstr in list)
            {
                tmp = tmp + kvstr;
            }

            //HMAC-SHA1
            HMACSHA1 hmacsha1 = new HMACSHA1(signatureKey);
            hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(tmp));
            /*
            hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(urlPath));
            foreach (string kvstr in list)
            {
                hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(kvstr));
            }
             */
            byte[] hash = hmacsha1.Hash;
            //TO HEX
            return BitConverter.ToString(hash).Replace("-", string.Empty).ToUpper();
        }
        public static string gethttpwithuri(string lingpaigeturl2)
        {
            string gethttpwithuriout = "";
            HttpWebRequest http23 = WebRequest.Create(lingpaigeturl2) as HttpWebRequest;
            // http23.ServicePoint.Expect100Continue = false;
            //http23.Referer = lingpaigeturl;
            http23.Method = "GET";
            http23.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            // http23.AllowAutoRedirect = true;
            //  http23.Credentials = System.Net.CredentialCache.DefaultCredentials;
            http23.KeepAlive = true;
            http23.AllowAutoRedirect = true;
            //  http23.ProtocolVersion = HttpVersion.Version10;
            //  http23.CookieContainer = MyCookieContainer;
            //using (Stream request2 = http23.GetRequestStream())
            //{
            //    try
            //    {
            //        // request2.Write(postData, 0, postData.Length);
            //    }
            //    catch
            //    {
            //        throw;
            //    }
            //    finally
            //    {
            //        request2.Close();
            //    }
            //}
            try
            {
                HttpWebResponse response12 = http23.GetResponse() as HttpWebResponse;
                StreamReader reader12 = new StreamReader(response12.GetResponseStream());
                // richTextBox3.Text += "你获取的uri返回是:" + reader12.ReadToEnd().ToString();
                gethttpwithuriout = reader12.ReadToEnd().ToString();
                return gethttpwithuriout;
            }
            catch (System.Exception ex)
            {
                return ex.ToString();
            }
            finally
            {

                ///  return gethttpwithuriout; 
            }
        }
        public static string uribuider(string apitpye, Dictionary<string, string> parm1, bool needaccess_token)
        {

            if (needaccess_token)
            {
                string oldori = "http://gw.open.1688.com/openapi/param2/1/cn.alibaba.open/" + apitpye + "/" + appKey + "?";
                string signolduri = "param2/1/cn.alibaba.open/" + apitpye + "/" + appKey;
                //  string signolduri2 = oldori + "param2/1/cn.alibaba.open/" + apitpye + "?";
                parm1.Add("access_token", acctk2.access_token);
                string signwithuri = sign(signolduri, parm1);
                foreach (KeyValuePair<string, string> kv in parm1)
                {
                    oldori += HttpUtility.HtmlEncode(kv.Key) + "=" + HttpUtility.HtmlEncode(kv.Value) + "&";
                }
                oldori += "_aop_signature=" + signwithuri;
                return oldori;
            }
            else
            {
                string oldori = "http://gw.open.1688.com/openapi/param2/1/cn.alibaba.open/" + apitpye + "/" + appKey + "?";
                string signolduri = "param2/1/cn.alibaba.open/" + apitpye + "/" + appKey;
                //  string signolduri2 = oldori + "param2/1/cn.alibaba.open/" + apitpye + "?";
                string signwithuri = sign(signolduri, parm1);
                foreach (KeyValuePair<string, string> kv in parm1)
                {
                    oldori += HttpUtility.HtmlEncode(kv.Key) + "=" + HttpUtility.HtmlEncode(kv.Value) + "&";
                }
                oldori += "_aop_signature=" + signwithuri;
                return oldori;
            }


            //return "";

        }
        public static string sarchkeyword(Dictionary<string, string> parm1)
        {
            string oldori = "http://s.1688.com/selloffer/offer_search.htm?";
            foreach (KeyValuePair<string, string> kv in parm1)
            {
                if (kv.Key == "keywords")
                {
                    oldori += kv.Key + "=" + HttpUtility.UrlEncode(kv.Value, Encoding.GetEncoding("GBK")).ToUpper() + "&";
                } 
                else
                {
                    oldori +=kv.Key + "=" + kv.Value + "&";
                }
               
            }
            oldori = oldori.TrimEnd('&');

            return oldori;
        }
        public static string gethttpwithurigbk(string lingpaigeturl2)
        {
            string gethttpwithuriout = "";
            HttpWebRequest http23 = WebRequest.Create(lingpaigeturl2) as HttpWebRequest;
            // http23.ServicePoint.Expect100Continue = false;
            //http23.Referer = lingpaigeturl;
            http23.Method = "GET";
            http23.ContentType = "application/x-www-form-urlencoded; charset=GBK";
            // http23.AllowAutoRedirect = true;
            //  http23.Credentials = System.Net.CredentialCache.DefaultCredentials;
            http23.KeepAlive = true;
            http23.AllowAutoRedirect = true;
            //  http23.ProtocolVersion = HttpVersion.Version10;
            //  http23.CookieContainer = MyCookieContainer;
            //using (Stream request2 = http23.GetRequestStream())
            //{
            //    try
            //    {
            //        // request2.Write(postData, 0, postData.Length);
            //    }
            //    catch
            //    {
            //        throw;
            //    }
            //    finally
            //    {
            //        request2.Close();
            //    }
            //}
            try
            {
                HttpWebResponse response12 = http23.GetResponse() as HttpWebResponse;
                StreamReader reader12 = new StreamReader(response12.GetResponseStream(),Encoding.GetEncoding("GBK"));
                // richTextBox3.Text += "你获取的uri返回是:" + reader12.ReadToEnd().ToString();
                gethttpwithuriout = reader12.ReadToEnd();
                return gethttpwithuriout;
            }
            catch (System.Exception ex)
            {
                return ex.ToString();
            }
            finally
            {

                ///  return gethttpwithuriout; 
            }
        }
    }
}
